"""
    create by IAmFiveHigh on 2023/9/7
"""
from typing import Optional

from list_node import ListNode


class Solution:
    @staticmethod
    def remove_elements(head: Optional[ListNode], val: int) -> Optional[ListNode]:
        dummpyNode = ListNode(None)
        dummpyNode.next = head

        pre = dummpyNode
        while pre.next is not None:
            if pre.next.val == val:
                pre.next = pre.next.next
            else:
                pre = pre.next
        return dummpyNode.next

    @staticmethod
    def remove_elements_by_recursion(head: Optional[ListNode], val: int) -> Optional[ListNode]:
        if head is None:
            return None

        node = Solution.remove_elements_by_recursion(head.next, val)
        if head.val == val:
            return node
        else:
            head.next = node
            return head
